package com.wuhongyu.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wuhongyu.entity.Mobilephone;
import com.wuhongyu.entity.MobilephoneCutsom;
import com.wuhongyu.mapper.MobilephoneMapper;
import com.wuhongyu.service.MobilePhoneService;
import com.wuhongyu.util.MyUtil;


@Service
public class MobilePhoneServiceImpl implements MobilePhoneService{

	@Autowired
	private MobilephoneMapper mobileMapper;
	@Override
	public List<MobilephoneCutsom> selectAll(Mobilephone mobilephone) {
		List<MobilephoneCutsom> phoneList = mobileMapper.selectAll(mobilephone);
		return phoneList;
	}

	@Override
	public MobilephoneCutsom queryById(Integer id) {
		MobilephoneCutsom phone = mobileMapper.selectById(id);
		return phone;
	}

	@Override
	public void updateOrInsert(Mobilephone mobilephone) {
		if(mobilephone.getId()!=null && !"".equals(mobilephone.getId())) {
			mobileMapper.updateByPrimaryKeySelective(mobilephone);
		}else {
			mobileMapper.insertSelective(mobilephone);
		}
	}

	@Override
	public void deleteById(Integer id) {
		MobilephoneCutsom phone = this.queryById(id);
		if(phone.getPic()!=null) {
			MyUtil.deleteSchedule(phone.getPic());
		}
		mobileMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void deleteBath(Integer[] ids) {
		for (Integer id : ids) {
			MobilephoneCutsom phone = this.queryById(id);
			if(phone.getPic()!=null) {
				MyUtil.deleteSchedule(phone.getPic());
			}
		}
		if(ids!=null) {
			List<Integer> idList = Arrays.asList(ids);
			mobileMapper.deleteBath(idList);
		}
		
	}

}
